//
//  CZMessageCell.m
//  QQ聊天界面
//
//  Created by MAC on 16-3-11.
//  Copyright (c) 2016年 MAC. All rights reserved.
//

#import "CZMessageCell.h"
#import "CZMessage.h"
#import "CZMessageFrame.h"

@interface CZMessageCell()


//将三个控件对象定义成成员变量
@property(nonatomic,weak)UILabel *lblTime;
@property(nonatomic,weak)UIImageView * imgViewIcon;
@property(nonatomic,weak)UIButton * btnText;


@end


@implementation CZMessageCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //创建子控件
        //1.显示时间的label控件
        UILabel * lblTime = [[UILabel alloc]init];

        lblTime.textAlignment = NSTextAlignmentCenter;
        
        lblTime.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:lblTime];
        self.lblTime = lblTime;
        
        //2.显示头像的imageView控件
        UIImageView * imgViewIcon = [[UIImageView alloc]init];
        [self.contentView addSubview:imgViewIcon];
        self.imgViewIcon = imgViewIcon;
        //3.显示正文的button控件
        UIButton * btnText = [[UIButton alloc]init];
        //设置正文文字颜色
        [btnText setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        //设置文字内容换行
        btnText.titleLabel.numberOfLines = 0;
        //设置button的内边距
        btnText.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
        
        [self.contentView addSubview:btnText];
        self.btnText = btnText;
    }
    self.backgroundColor = [UIColor clearColor];
    return self;
}

//重写frame模型的set方法
-(void)setMessageFrame:(CZMessageFrame *)messageFrame
{
    _messageFrame = messageFrame;
    //获取数据模型
    CZMessage * message = messageFrame.message;
    //分别设置每一个控件的数据和frame
    //1.设置时间label的数据和frame
    self.lblTime.text = message.time;
    self.lblTime.frame = messageFrame.timeFrame;
    
    //2.设置头像的图片和frame
    //获取图片的名字
    NSString * iconImg = message.type == CZMessageTypeMe?@"me":@"other";
    self.imgViewIcon.image = [UIImage imageNamed:iconImg];
    self.imgViewIcon.frame = messageFrame.iconFrame;
    
    
    
    //设置背景图片
    //设置字体颜色
    //[self.btnText setTitle:message.text forState:UIControlStateNormal];
    //UIColor *fontColor = (message.type == CZMessageTypeMe) ? [UIColor whiteColor] : [UIColor blueColor] ;
    //[self.btnText setTitleColor:fontColor forState:UIControlStateNormal];
    
    
    
    //NSString *iconName = (message.type == CZMessageTypeMe) ? @"chat_send_press_pic" :@"chat_recive_press_pic";
    //[self.btnText setImage:[UIImage imageNamed:iconName] forState:UIControlStateNormal];
    //要将图片进行拉伸，拉到刚好包含文字的大小
    //UIImage *iconImage = [UIImage imageNamed:iconName];
    //裁剪方法对背景图片进行处理
    //iconImage = [iconImage stretchableImageWithLeftCapWidth:iconImage.size.width*0.5 topCapHeight:iconImage.size.height*0.5];
    //将背景图片贴合按钮的大小
    
    //4.设置正文背景
    NSString* imgNor, *imgHighLighted;
    if(message.type == CZMessageTypeMe)
    {
        imgNor = @"chat_send_nor";
        imgHighLighted = @"chat_send_press_pic";
        //自己发的消息，设置颜色为白色
        [self.btnText setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        imgNor = @"chat_recive_nor";
        imgHighLighted = @"chat_recive_press_pic";
        [self.btnText setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    //加载图片
    UIImage* imageNormal = [UIImage imageNamed:imgNor];
    UIImage* imageHighLighted = [UIImage imageNamed:imgHighLighted];
    
    //用平铺的方式拉伸图片
    imageNormal = [imageNormal stretchableImageWithLeftCapWidth:imageNormal.size.width*0.5 topCapHeight:imageNormal.size.height*0.5];
    
    imageHighLighted = [imageHighLighted stretchableImageWithLeftCapWidth:imageHighLighted.size.width*0.5 topCapHeight:imageHighLighted.size.height*0.5];
    
    //将图片设置背景图片给按钮
    
    //3.设置正文消息的数据和frame
    [self.btnText setBackgroundImage:imageNormal forState:UIControlStateNormal];
    [self.btnText setBackgroundImage:imageHighLighted forState:UIControlStateHighlighted];
    
    [self.btnText setTitle:message.text forState:UIControlStateNormal];
    self.btnText.frame = messageFrame.textFrame ;
  
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
